package com.hay.oss.eos;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * eos配置
 * @author LiMouRen
 * @date 2023/7/26
 */
@Configuration
@EnableConfigurationProperties({EosProperties.class})
@ConditionalOnProperty(name = "eos.enable",havingValue = "true")
public class EosConfiguration {

    @Bean
    public EosTemplate eosTemplate(EosProperties eosProperties) {
        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(eosProperties.getEndpoint(), eosProperties.getRegion());
        BasicAWSCredentials credentials = new BasicAWSCredentials(eosProperties.getAccessKey(), eosProperties.getSecretKey());
        AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
        AmazonS3 client = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(endpointConfiguration)
                .withCredentials(credentialsProvider).build();
        return new EosTemplate(eosProperties,client);
    }

}
